package com.wtz.temptest.process;

import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Date: 2020/11/9
 * Company: www.dtstack.com
 * add git test
 * @author tiezhu
 */
public class JavaProcessBuilder {
    private String mainClass;
    private int startingHeapSizeInMegabytes = 40;
    private int maximumHeapSizeMegabytes = 128;
    private String workingDirectory;
    private List<String> mainClassArguments = new ArrayList<>();
    private List<String> classPathEntries = new ArrayList<>();
    private String javaRuntime = "java";

    public String getMainClass() {
        return mainClass;
    }

    public void setMainClass(String mainClass) {
        this.mainClass = mainClass;
    }

    public int getStartingHeapSizeInMegabytes() {
        return startingHeapSizeInMegabytes;
    }

    public void setStartingHeapSizeInMegabytes(int startingHeapSizeInMegabytes) {
        this.startingHeapSizeInMegabytes = startingHeapSizeInMegabytes;
    }

    public int getMaximumHeapSizeMegabytes() {
        return maximumHeapSizeMegabytes;
    }

    public void setMaximumHeapSizeMegabytes(int maximumHeapSizeMegabytes) {
        this.maximumHeapSizeMegabytes = maximumHeapSizeMegabytes;
    }

    public String getWorkingDirectory() {
        return workingDirectory;
    }

    public void setWorkingDirectory(String workingDirectory) {
        this.workingDirectory = workingDirectory;
    }

    public List<String> getMainClassArguments() {
        return mainClassArguments;
    }

    public void setMainClassArguments(List<String> mainClassArguments) {
        this.mainClassArguments = mainClassArguments;
    }

    public void addMainClassArguments(String argument) {
        this.mainClassArguments.add(argument);
    }

    public String getClassPathEntries() {
        StringBuilder builder = new StringBuilder();
        int count = 0;
        final int totalSize = this.classPathEntries.size();
        for (String classPathEntry : classPathEntries) {
            builder.append(classPathEntry);
            count++;
            if (count < totalSize) {
                builder.append(System.getProperty("path.separator"));
            }
        }
        return builder.toString();
    }

    public void setClassPathEntries(List<String> classPathEntries) {
        this.classPathEntries = classPathEntries;
    }

    public void addClassPath(String classPath) {
        this.classPathEntries.add(classPath);
    }

    public String getJavaRuntime() {
        return javaRuntime;
    }

    public void setJavaRuntime(String javaRuntime) {
        this.javaRuntime = javaRuntime;
    }

    public Process startProcess() throws IOException {
        List<String> argumentsList = new ArrayList<>();
        argumentsList.add(this.javaRuntime);
        argumentsList.add(MessageFormat.format("-Xms{0}M", String.valueOf(this.startingHeapSizeInMegabytes)));
        argumentsList.add(MessageFormat.format("-Xms{0}M", String.valueOf(this.maximumHeapSizeMegabytes)));
        argumentsList.add("-classpath");
        argumentsList.add(getClassPathEntries());
        argumentsList.add(this.mainClass);
        argumentsList.addAll(mainClassArguments);
        ProcessBuilder processBuilder = new ProcessBuilder(argumentsList);
        processBuilder.redirectErrorStream(true);
        processBuilder.directory(new File(this.workingDirectory));
        return processBuilder.start();
    }
}
